<?php

/**
 * Created by PhpStorm.
 * User: raymon
 * Date: 17-8-7
 * Time: 下午3:07
 */
class Column extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->auth('admin');
        $this->_add_style_sheet("dataTables", base_url("assets/DataTables/css/dataTables.bootstrap4.min.css"));
        $this->_add_script_file("dataTables", base_url("assets/DataTables/js/jquery.dataTables.min.js"));
        $this->_add_script_file("dataTables Bootstrap", base_url("assets/DataTables/js/dataTables.bootstrap.min.js"));

        $this->load->model('column_model');
    }

    public function index()
    {
        $data = array();
        $this->_view('admin/column/column.twig', $data, 'admin', 'admin/column');
    }

    public function json()
    {
        $json["data"] = $this->column_model->get_all('column');
        print_r(json_encode($json));
    }

    public function add()
    {
        $data = array();
        if ($this->input->post()) {
            $this->load->library('form_validation');
            $return = array();
            if ($this->form_validation->run('column') == FALSE) {
                $return['state'] = false;
                $this->form_validation->set_error_delimiters('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>', '</div>'); //设置错误信息的定界符
                $return['data'] = validation_errors();
            } else {
                $post = $this->input->post();
                if ($this->column_model->add('column', $post)) {
                    $return['state'] = true;
                    $return['data'] = $this->classesBg('success','新增成功！');
                } else {
                    $return['state'] = false;
                    $return['data'] = $this->classesBg('dismissible','数据更新失败！');
                }
            }
            print_r(json_encode($return));//输出json
        } else {
            $this->_view('admin/column/column-add.twig', $data);
        }
    }

    public function change($id = '')
    {
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('', ''); //去掉错误信息的<p>定界符
        $return = array();
        if ($this->form_validation->run('column') == FALSE) {
            $return['state'] = false;
            $return['data'] = validation_errors();
        } else {
            $post = $this->input->post();
            if ($this->column_model->update('column', $id, $post)) {
                $return['state'] = true;
            } else {
                $return['state'] = false;
                $return['data'] = '数据更新失败!';
            }
        }
        print_r(json_encode($return));

    }

    public function del($id = '')
    {
        $data = array();
        $column = $this->column_model->get('column', $id);
        if (empty($column)) {
            exit('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>严重错误，该栏目不存在!</div>');
        }
        if ($this->input->post()) {
            $return = array();
            if ($this->column_model->del('column', $id)) {
                $return['state'] = true;
                $return['data'] = '删除成功！';
            } else {
                $return['state'] = false;
                $return['data'] = $this->classesBg('dismissible','数据更新失败！');
            }
            print_r(json_encode($return));
        } else {
            $data['input'] = &$column;
            $this->_view('admin/column/column-del.twig', $data);
        }
    }
}